package com.example.smartcity.ui.dashboard;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartcity.R;

import com.example.smartcity.ui.Adapter.SmartBarRecycleViewAdapter;
import com.example.smartcity.ui.Bean.SmartBarStopBaseMesBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SmartBarMessage extends AppCompatActivity {

    private TextView TV_start;
    private TextView end;
    private TextView TV_price;
    private TextView TV_mileage;
    private ImageView IV_topicFunctionIcon;
    private RecyclerView recycleView;
    private Button nextOne;
    SmartBarStopBaseMesBean.DataBean dataBean;
    ArrayList<String> mes;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    TV_start.setText(dataBean.getFirst());
                    end.setText(dataBean.getEnd());
                    TV_price.setText("￥"+dataBean.getPrice()+"元");
                    TV_mileage.setText("全程"+dataBean.getMileage()+"Km");
                    LinearLayoutManager manager = new LinearLayoutManager(SmartBarMessage.this);
                    manager.setOrientation(RecyclerView.HORIZONTAL);
                    recycleView.setLayoutManager(manager);
                    recycleView.setAdapter(new SmartBarRecycleViewAdapter(mes,R.layout.smartbarstopmessagelayout));
                    break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_bar_message);
        TV_start = (TextView) findViewById(R.id.TV_start);
        end = (TextView) findViewById(R.id.end);
        TV_price = (TextView) findViewById(R.id.TV_CSprice);
        TV_mileage = (TextView) findViewById(R.id.TV_CSmileage);
        IV_topicFunctionIcon = (ImageView) findViewById(R.id.IV_topicFunctionIcon);
        recycleView = (RecyclerView) findViewById(R.id.CSrecycleView);
        nextOne = (Button) findViewById(R.id.nextOne);
        initView();
        setNextOne();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        Intent intent = getIntent();
        mes = intent.getStringArrayListExtra("StopMessageList");
        String LinesId = intent.getStringExtra("LinesId");
        String newUrl = OkHttpUtils.getBaseUrl(SmartBarMessage.this) + "/userinfo/lines/" + LinesId;
        OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                SmartBarStopBaseMesBean bean = gson.fromJson(response.body().string(),SmartBarStopBaseMesBean.class);
                dataBean = bean.getData();
                Message message = new Message();
                message.what =0;
                handler.sendMessage(message);
            }
        });
    }

    public void setNextOne(){
        nextOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmartBarMessage.this,SmartBarSetSelftMessage.class);
                intent.putStringArrayListExtra("stop",mes);
                intent.putExtra("start",mes.get(0));
                intent.putExtra("end",mes.get(mes.size()-1));
                startActivity(intent);
            }
        });
    }
}